/**
 * 
 */
package com.wutianyi.arithmetic;

import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;


/**
 * 公钥密码
 * rsa
 * @author wutianyi
 *
 */
public class MyPublic {
	
	public static void main(String[] args) throws UnsupportedEncodingException, NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
		

		String message = "wutianyi_public";
		byte[] plainText = message.getBytes("utf8");
		
		KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA");
		keyPairGenerator.initialize(1024);
		KeyPair keyPair = keyPairGenerator.generateKeyPair();
		
		Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding");
		cipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());
		byte[] cipherText = cipher.doFinal(plainText);
		System.out.println(new String(cipherText,"utf8"));
		cipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate());
		System.out.println(new String(cipher.doFinal(cipherText), "utf8"));
	}
}
